package m.meiweitech.com.rocket.helper

import android.annotation.SuppressLint
import android.app.Activity
import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothManager
import android.content.Context
import com.orhanobut.logger.Logger
import cpcl.PrinterHelper
import m.meiweitech.com.rocket.app.App
import m.meiweitech.com.rocket.entities.DeviceInfo
import m.meiweitech.com.rocket.entities.FlutterDeviceInfo
import m.meiweitech.com.rocket.entities.MachineInfoList
import m.meiweitech.com.rocket.entities.PrinterType
import m.meiweitech.com.rocket.util.MacCheckUtil
import m.meiweitech.com.rocket.util.SpUtil

/**
 * @author WEN
 * @Description:
 * @date 2022/1/2 21:31
 */
@SuppressLint("StaticFieldLeak")
object PipeHelper {

    private lateinit var mContext: Context

    fun initialize(context:Context){
        mContext = context
    }

    /**
     * 子线程工作
     */
    fun connect(activity: Activity,mac: String = SpUtil.MMKV_mac, printerType: PrinterType = PrinterType.HM) {

        when(printerType){

            PrinterType.HM -> hmConnect(activity,mac)

        }

    }

    @SuppressLint("MissingPermission")
    private fun hmConnect(activity: Activity,mac:String){
        try {
            val code = PrinterHelper.portOpenBT(mContext, mac)
            if(code!=0){
                val message = when (code) {
                    -1 -> {
                        "连接超时"
                    }
                    -2 -> {
                        "蓝⽛地址格式错误"
                    }
                    -3 -> {
                        "打印机与SDK不匹配（握⼿不通过）"
                    }
                    else -> {
                        "未知错误"
                    }
                }
                throw Exception("HM打印机连接错误,错误代码:$code,错误信息:$message")
            }
            val isConnect = MacCheckUtil.isBtConDeviceByMac(SpUtil.MMKV_mac)
            val adapter = (App.CONTEXT.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager).adapter
            val bluetoothDevice = adapter.getRemoteDevice(SpUtil.MMKV_mac)
            val series = if (bluetoothDevice.name == null) {
                MachineInfoList[0].series
            } else {
                MachineInfoList.firstOrNull {
                    bluetoothDevice.name.contains(it.series)
                }.run {
                    this?.series ?: MachineInfoList[1].series
                }
            }
            SpUtil.MMKV_mac = mac
            SpUtil.MMKV_device_name = bluetoothDevice.name

            BlueConnectStatusListener.notifyStatusAll(activity,FlutterDeviceInfo(bluetoothDevice, series, isConnect))
        } catch (e: Exception) {
            Logger.i("由于${e.message}连接失败")
        }
    }

    /**
     * 断开连接
     */
    @SuppressLint("MissingPermission")
    fun disconnect() {
        val adapter = (App.CONTEXT.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager).adapter
        val bluetoothDevice = adapter.getRemoteDevice(SpUtil.MMKV_mac)
        if(bluetoothDevice!=null){
            when(PrinterType.strParsePrinterType(bluetoothDevice.name)){

                PrinterType.HM -> {
                    PrinterHelper.portClose()
                    BlueConnectStatusListener.notifyDisconnectStatusAll()
                }
            }
        }
    }

}